package com.b2b.controller;


import com.b2b.model.common.ResponseBean;
import com.b2b.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/upload")
@PropertySource("classpath:config.properties")
public class FileController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    @Autowired
    FileUtil fileUtil;

    @PostMapping("/img")
    public ResponseBean saveImg(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            if (Objects.requireNonNull(file.getContentType()).contains("image")) {
                try {
                    // 获取图片的文件名
                    String fileName = file.getOriginalFilename();
                    assert fileName != null;
                    String[] extended = new String[]{".jpg", ".jpeg", ".png", ".gif"};
                    String extendedName = fileName.substring(fileName.lastIndexOf('.'));
                    if(extendedName.equals(extended[0]) | extendedName.equals(extended[1]) |
                            extendedName.equals(extended[2]) | extendedName.equals(extended[3])) {
                        String newFileName = fileUtil.getFileName(fileName, extendedName);
                        // 文件路径
                        String filePath = "D:" + File.separator + "images" + fileUtil.getFilePath();
                        File dest = new File(filePath, newFileName);
                        if (!dest.getParentFile().exists()) {
                            dest.getParentFile().mkdirs();
                        }
                        // 上传到指定目录
                        file.transferTo(dest);
                        return new ResponseBean(HttpStatus.OK.value(), "上传成功", filePath + newFileName);
                    }else {
                        return new ResponseBean(400, "上传的文件不是指定格式", null);
                    }
                } catch (IOException e) {
                    return new ResponseBean(500, "上传失败", null);
                }
            } else {
                return new ResponseBean(400, "上传的文件不是图片类型，请重新上传!", null);
            }
        } else {
            return new ResponseBean(500, "上传失败，请选择要上传的图片!", null);
        }
    }

    @PostMapping("/contract")
    public ResponseBean saveContract(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                // 文件名
                String fileName = file.getOriginalFilename();
                assert fileName != null;
                String extendedName = fileName.substring(fileName.lastIndexOf('.'));
                if(extendedName.equals(".doc") | extendedName.equals(".pdf")) {
                    String newFileName = fileUtil.getFileName(fileName, extendedName);
                    // 文件路径
                    String filePath = "D:" + File.separator + "contract" + fileUtil.getFilePath();
                    File dest = new File(filePath, newFileName);
                    if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdirs();
                    }
                    // 上传到指定目录
                    file.transferTo(dest);
                }else {
                    return new ResponseBean(400, "上传的文件不是pdf或word文件", null);
                }
            } catch (IOException e) {
                return new ResponseBean(500, "上传失败", null);
            }
            return new ResponseBean(200, "上传成功", null);
        } else {
            return new ResponseBean(500, "上传失败，请选择要上传的文件!", null);
        }
    }

}
